<?php

use yii\helpers\Html;
use yii\widgets\ActiveForm;

/* @var $this yii\web\View */
/* @var $model common\models\UserCompany */
/* @var $form yii\widgets\ActiveForm */
?>
<?=Html::jsFile('/static/common/js/ajaxfileupload.js')?>
<style>
    .photo {
        width: 100%;
        height: 150px;
    }
    .photo img {
        max-width: 140px;
        max-height: 140px;
    }
</style>
<div class="user-company-form">
    <?php $form = ActiveForm::begin(); ?>
    <table class="table">
        <tr>
            <td width="50%">
                <?= $form->field($model, 'name')->textInput(['maxlength' => true]) ?>
            </td>
            <td>
                <?= $form->field($model, 'type')->dropDownList(\common\helpers\TStringHelper::getCompanyType()) ?>
            </td>
        </tr>
        <tr>
            <td><?= $form->field($model, 'product')->textInput(['maxlength' => true]) ?></td>
            <td><?= $form->field($model, 'category')->textInput(['maxlength' => true]) ?></td>
        </tr>
        <tr>
            <td><?= $form->field($model, 'fax')->textInput(['maxlength' => true]) ?></td>
            <td><?= $form->field($model, 'telphone')->textInput(['maxlength' => true]) ?></td>
        </tr>
        <tr>
            <td><?= $form->field($model, 'contact_user')->textInput(['maxlength' => true]) ?></td>
            <td><?= $form->field($model, 'contact_mobile')->textInput(['maxlength' => true]) ?></td>
        </tr>
        <tr>
            <td><?= $form->field($model, 'url')->textInput(['maxlength' => true]) ?></td>
            <td>
                <?= $form->field($model, 'business')->radioList(array(
                    '普通营业执照' => '普通营业执照',
                    '企业三证合一' => '企业三证合一（自2015年10月1日起登记机关颁发的含统一信用代码的营业执照）',
                )) ?>
            </td>
        </tr>
        <tr>
            <td>
                <?= $form->field($model, 'business_photo')->textInput(['id' => 'business_photo']) ?>
                <input type="file" name="Filedata" class="file_upload_attach" id="business" data-id="attach">
                <div class="photo">
                    <img src="<?=$model->business_photo?>" class="img-thumbnail" id="business_image" />
                </div>
            </td>
            <td>
                <?= $form->field($model, 'origanization_code_photo')->textInput(['id' => 'origanization_code_photo']) ?>
                <input type="file" name="Filedata" class="file_upload_attach" id="origanization_code" data-id="attach">
                <div class="photo">
                    <img src="<?=$model->origanization_code_photo?>" class="img-thumbnail" id="origanization_code_image" />
                </div>
            </td>
        </tr>
        <tr>
            <td>
                <?= $form->field($model, 'certificate_photo')->textInput(['id' => 'certificate_photo']) ?>
                <input type="file" name="Filedata" class="file_upload_attach" id="certificate" data-id="attach">
                <div class="photo">
                    <img src="<?=$model->certificate_photo?>" class="img-thumbnail" id="certificate_image" />
                </div>
            </td>
            <td>
                <?= $form->field($model, 'opening_permit_photo')->textInput(['id' => 'opening_permit_photo']) ?>
                <input type="file" name="Filedata" class="file_upload_attach" id="opening_permit" data-id="attach">
                <div class="photo">
                    <img src="<?=$model->opening_permit_photo?>" class="img-thumbnail" id="opening_permit_image" />
                </div>
            </td>
        </tr>
        <tr>
            <td><?= $form->field($model, 'status')->dropDownList(\common\helpers\TStringHelper::getCompanyStatusText()) ?></td>
            <td></td>
        </tr>
        <tr>
            <td colspan="2"><?= $form->field($model, 'reason')->textarea(['maxlength' => true]) ?></td>
        </tr>
    </table>
    <div class="form-group">
        <?= Html::submitButton($model->isNewRecord ? Yii::t('common', 'create') : Yii::t('common', 'update'), ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
    </div>

    <?php ActiveForm::end(); ?>

</div>
<script>
$(function () {
    $(".file_upload_attach").change(function (e) {
        var val = e.target.value;
        var point = val.lastIndexOf(".");
        var type = val.substr(point).toLowerCase();
        if (type != ".jpg" && type != ".jpeg" && type != ".png") {
            alert('系统目前只支持.jpg、.jpeg、.png文件格式');
            return false;
        }
        var file_id = $(this).attr('id');
        $.ajaxFileUpload({
            //处理图片的脚本路径
            url: '<?=\yii\helpers\Url::toRoute(['common/upload-thumb', 'event' => 1, 'dir' => 'user_company'])?>',
            type: 'post',       //提交的方式
            secureuri: false,   //是否启用安全提交
            fileElementId: file_id,     //file控件ID
            dataType: 'json',  //服务器返回的数据类型
            success: function (data, status) {  //提交成功后自动执行的处理函数
                if (data.error) {
                    alert(data.msg);
                    return false;
                } else {
                    $("#"+file_id+'_photo').val(data.fileUrl);
                    $("#"+file_id+'_image').attr('src', data.fileUrl);
                }
            },
            error: function (data, status, e) {   //提交失败自动执行的处理函数
                alert(e);
            }
        })
    })
})
</script>
